var t = getApp(), a = require("../../utils/util.js"), e = require("../../nitro.js"), o = a.config("categories"), n = function(t, e) {
    return t.forEach(function(t) {
        t.packages = [], t.gallery = [], e.forEach(function(e) {
            e.selected = !1, console.log("pkg.gallery count", e.gallery.length), e.gallery = a.shuffle(e.gallery).slice(0, 15), 
            e.optionsObject = {}, e.options.forEach(function(t) {
                e.optionsObject[t.title] = t.content;
            }), e.category && e.category.id == t.id && (t.packages.push(e), console.log("gallery count", t.gallery.concat(e.gallery).length), 
            t.gallery = a.shuffle(t.gallery.concat(e.gallery)).slice(0, 15));
        }), t.hasSubItems = t.title.indexOf("小主题") > -1, t.title.includes("小主题") ? (t.mp_info = o.theme, 
        t.is_themes = !0) : t.title.toLowerCase().includes("look") ? t.mp_info = o.look : t.title.toLowerCase().includes("face") ? t.mp_info = o.face : t.title.toLowerCase().includes("baby") && (t.mp_info = o.baby);
    }), t.filter(function(t) {
        return t.publish;
    });
};

Page({
    data: {
        isIpx: t.globalData.isIpx,
        storeId: "",
        disabledScroll: !1,
        categories: [],
        gallery: {},
        vipBanner: "https://nitro-prod.oss-cn-hangzhou.aliyuncs.com/public/mp-vip-banner.png",
        imgUrls: [],
        artworkTabs: !1,
        indicatorDots: !1,
        autoplay: !1,
        interval: 5e3,
        duration: 1e3
    },
    onLoad: function(o) {
        t.globalData.ts = +new Date();
        var n = e.getStore()._id, i = o.category_id, r = a.artworkTabs(i), c = r.find(function(t) {
            return t.category_id === i;
        });
        console.info("options", n), wx.showLoading({}), this.setData({
            storeId: n,
            categoryId: i,
            artworkTabs: r,
            class: c.class
        }), this.initData(n);
    },
    onReady: function() {
        wx.setNavigationBarColor({
            frontColor: "#000000",
            backgroundColor: "#ffffff",
            animation: {
                duration: 400,
                timingFunc: "easeIn"
            }
        });
    },
    onShow: function() {},
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {
        return {
            title: "尼可儿童摄影",
            path: "/pages/stores/stores"
        };
    },
    initData: function(o) {
        var i = this, r = o || e.getStore()._id;
        e.fetchProducts(r).then(function(o) {
            var r = n(o.categories, o.items), c = r.find(function(t) {
                return t.is_themes;
            }).packages.map(function(t) {
                return {
                    title: t.title,
                    id: t._id,
                    cover: t.gallery.length && t.gallery[0].url.raw,
                    category_id: t.category._id
                };
            }), s = a.chunkArray(c.filter(function(t) {
                return t.cover;
            }), 6), l = i.calcPackageInfo(r, i.data.categoryId);
            i.setData({
                store: e.getStore(),
                packageLoaded: !0,
                packageId: l.packages[0].id,
                packageInfo: l,
                categories: r,
                themeGroups: s
            }), i.onSelectPackage(null, i.data.categoryId, i.data.packageId), wx.setNavigationBarTitle({
                title: "" + i.data.store.name
            }), wx.hideLoading(), console.info("duration: product-detail", +new Date() - t.globalData.ts);
        });
    },
    calcPackageInfo: function(t, a) {
        var e = t.find(function(t) {
            return t._id === a;
        }), o = {
            special_explanation: e.special_explanation,
            mp_info: e.mp_info,
            packages: []
        };
        return e.packages.map(function(t) {
            ("小主题" === e.title && t.title.includes("T") || "小主题" !== e.title) && o.packages.push({
                title: t.title.substr(0, 2),
                id: t._id,
                options: t.options,
                price: t.price,
                description: t.description.replace(/\\r\\n/g, "\n")
            });
        }), o.packages.sort(function(t, a) {
            return t.title > a.title;
        }), o;
    },
    onPreviewSlides: function(t) {
        var a = t.currentTarget.dataset.url, e = t.currentTarget.dataset.category, o = t.currentTarget.dataset.package, n = [];
        this.data.categories.forEach(function(t) {
            t.id === e && (t.hasSubItems ? t.packages.forEach(function(t) {
                t.id === o && t.gallery.forEach(function(t) {
                    n.push(t.url.raw);
                });
            }) : t.gallery.forEach(function(t) {
                n.push(t.url.raw);
            }));
        }), wx.previewImage({
            current: a,
            urls: n
        });
    },
    loadThemePhotos: function(t) {
        var a = t.currentTarget.dataset.id;
        console.log("e.currentTarget.dataset", t.currentTarget.dataset), wx.navigateTo({
            url: "/pages/themes/themes?category_id=" + a,
            success: function(t) {},
            fail: function(t) {},
            complete: function(t) {}
        });
    },
    loadMorePhotos: function(t) {
        return;
    },
    onSwitchTab: function(t) {
        var e = t.currentTarget.dataset.categoryid, o = this.calcPackageInfo(this.data.categories, e), n = a.artworkTabs(e).find(function(t) {
            return t.category_id === e;
        });
        this.setData({
            categoryId: e,
            artworkTabs: a.artworkTabs(e),
            packageInfo: o,
            packageId: o.packages[0].id,
            class: n.class
        });
    },
    onSelectPackage: function(a, e, o) {
        var n = a && a.currentTarget.dataset.packageid || o, i = a && a.currentTarget.dataset.categoryid || e;
        console.info("package_id", n), console.info("category_id", i);
        var r = this.data.categories.find(function(t) {
            return t._id === i;
        }).packages.filter(function(t) {
            return t._id === n;
        }), c = 0, s = 0;
        r.forEach(function(a) {
            console.info("check inventory_restrictions", a.inventory_restrictions), "weekend" === a.inventory_restrictions ? t.globalData.cart.inventory_restrictions = "weekend" : delete t.globalData.cart.inventory_restrictions, 
            c += a.price, s += a.advance_price;
        }), this.setData({
            packageId: n
        }), t.globalData.campaign && "bday1810" === t.globalData.campaign.id && (console.info("有优惠活动", t.globalData.campaign), 
        c *= t.globalData.campaign.discount), t.globalData.cart.items = r, t.globalData.cart.totalPrice = c, 
        t.globalData.cart.prepaidPrice = s, console.info("app.globalData.cart", t.globalData.cart);
    },
    onChooseDate: function(t) {
        wx.navigateTo({
            url: "/pages/schedule/schedule?store_id=" + this.data.storeId,
            success: function(t) {},
            fail: function(t) {},
            complete: function(t) {}
        });
    }
});